﻿using Ldt.DataEngine.RuleAssemble;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.DataModel.DynamicData;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;

namespace LdtDataEngin.ModuleDetail.RegCommon.InfoUnitAnalysis
{
    ///<summary>
    /// 类名称  : UintAnalysis
    /// 功能    : 分析流程单元解析
    /// 更新人  : ljx 
    ///</summary> 
    public class UintAnalysis
    {

        /// <summary>
        /// 动态数据
        /// </summary>
        public DataContext dynamicData { get; set; }


        public UintAnalysis() { }


        /// <summary>
        /// 进行执行信息单元内容
        /// </summary>
        /// <param name="UnitCode">执行信息单元的主键ID</param>
        /// <param name="OriginalTxt">原文集合</param>
        /// <param name="spokePersonJoinIsNull">回调函数判断是否为空</param>
        /// <returns></returns>
        public IDictionary<string, List<string>> ExtUnitAnalysis(string UnitCode, IList<string> OriginalTxt,RuleInfoModel ruleInfo, Verification_spokePersonIsNull spokePersonJoinIsNull)
        {
            spokePersonJoinIsNull();
            //获取实体对象数据;
            DataTable EntityInfoData = dynamicData.DataLayerAns.SpokePersonJoin;
            string parasstr = GetParameterStr(UnitCode, EntityInfoData);
            Stopwatch swtext = new Stopwatch();
            swtext.Start();
            Dictionary<string, List<string>> valuePairs = RuleParseClass.GetParseDicList(UnitCode, OriginalTxt.ToList(), parasstr);
            if (valuePairs.Count>0)
            {
                valuePairs.Add("paragraphorg", OriginalTxt.ToList());
            }
            swtext.Stop();
            if (swtext.ElapsedMilliseconds > 3000)
            {
                string regParam = !string.IsNullOrWhiteSpace(parasstr) ? $"正则条件：{parasstr}," : string.Empty;
                string content = $"正则编号为【{UnitCode}】的正则执行时长{swtext.ElapsedMilliseconds}毫秒,{regParam}原文：{string.Join("", OriginalTxt.ToList())}";
                dynamicData.InteractiveShow?.LogShow(content);
                //WriteLog("分析总时长" + swtext.ElapsedMilliseconds.ToString("0.00") + ",正则编号:" + regcode + "原文：" + text + "正则条件:" + tj, "\\log\\log.txt", false);
            }
            
            ruleInfo.RuleCode = UnitCode;
            ruleInfo.RuleParameter = parasstr;
            return valuePairs;

        }


        /// <summary>
        /// 进行将当前信息单元的参数数据进行拼接
        /// </summary>
        /// <param name="UnitCode">信息单元主键ID</param>
        /// <param name="EntityInfoData">实体数据集合</param>
        /// <returns></returns>
        private string GetParameterStr(string UnitCode, DataTable EntityInfoData)
        {
            XmlUtilCommon xmlUtil = new XmlUtilCommon();
            //获取参数配置信息
            var ParameterList = RuleParseClass.GetConfigList(UnitCode);

            List<string> parasAll = new List<string>();
            foreach (var mo in ParameterList)
            {
                switch (mo.PLType.ToString())
                {
                    case "1":
                        if (!string.IsNullOrWhiteSpace(mo.PLSourceWhere) && EntityInfoData.Columns.Contains(mo.PLSource))
                        {
                            try
                            {
                                DataRow[] dtrows = EntityInfoData.Select(mo.PLSourceWhere);
                                List<string> cloumns = dtrows.Select(a => a.Field<string>(mo.PLSource.ToLower())).ToList();
                                List<string> paras = new List<string>();
                                foreach (var cloumn in cloumns)
                                {
                                    if (!string.IsNullOrWhiteSpace(cloumn))
                                    {
                                        paras.Add(cloumn);
                                    }
                                }
                                if (paras.Count > 0)
                                {
                                    parasAll.Add(mo.PLName + ":" + string.Join("|", paras));
                                }
                            }
                            catch (Exception ex)
                            {
                                dynamicData.InteractiveShow.LogShow("\tUintAnalysis下GetParameterStr函数报错" + ex.Message, LogMessageType.Error);
                            }
                        }
                        break;
                    case "2":
                        string xpath = "", valueList = "";
                        // 拼接xpath
                        if (string.IsNullOrEmpty(mo.PLSource) || string.IsNullOrEmpty(mo.PLSourceWhere))
                        {
                            return "";
                        }
                        xpath = mo.PLSourceWhere + @"/@" + mo.PLSource;
                        // 先将要素节点列表转化成一个XmlDocument输出
                        XmlDocument doc = new XmlDocument();
                        doc = xmlUtil.AnalyticalXmlDocument(dynamicData.DataLayerAns.ElementNodeModelsList);
                        // 利用xpath得到筛选的节点
                        XmlNodeList elementNodes = doc.SelectNodes(xpath);
                        foreach (XmlNode item in elementNodes)
                        {
                            if (string.IsNullOrEmpty(valueList))
                            {
                                valueList = item.Value;
                            }
                            else
                            {
                                valueList += "|" + item.Value;
                            }
                        }
                        parasAll.Add(mo.PLName + ":" + valueList);
                        break;

                    case "3":
                        if (dynamicData.DataLayerAns.CaseCausesList!=null || dynamicData.DataLayerAns.CaseCausesList.Count>0)
                        {
                            List<string> causeNameList = new List<string>();
                            foreach (var casusemodel in dynamicData.DataLayerAns.CaseCausesList)
                            {
                                causeNameList.Add(casusemodel.CC_Name);
                            }
                            parasAll.Add(mo.PLName + ":" + string.Join("|", causeNameList));
                        }
                        break;
                }

            }

            string parstr = string.Join(",", parasAll);
            return parstr;
        }



    }
}
